package com.tushare.controller;

import com.tushare.entity.HkHold;
import com.tushare.entity.Margin;
import com.tushare.entity.MarginDetail;
import com.tushare.network.WapResponse;
import com.tushare.util.ServiceUtil;
import com.tushare.util.TransformClassUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@CrossOrigin("*")
public class CharacteristicController {
    private static final String TAG = "CharacteristicController";
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    private final TransformClassUtil transformClassUtil = new TransformClassUtil();
    private final ServiceUtil serviceUtil = new ServiceUtil();
    @Autowired
    RestTemplate restTemplate;

    /**
     * 沪深港股通持股明细
     */
    @GetMapping("hk_hold")
    public List<HkHold> getHkHold(@RequestParam(value = "ts_code", required = false) String ts_code,
                                  @RequestParam(value = "trade_date", required = false) Integer trade_date) {
        serviceUtil.initWapRequest("hk_hold");
        if (ts_code != null) {
            serviceUtil.appendField("ts_code", ts_code);
        } else {
            if (trade_date != null) {
                serviceUtil.appendField("trade_date", trade_date);
            } else {
                return null;
            }
        }
        WapResponse.WapData data = serviceUtil.getNetworkData(restTemplate);
        return data == null ? null : transformClassUtil.initList(data, HkHold.class)
                .stream().filter(hold -> "SH".equals(hold.getExchange()) || "SZ".equals(hold.getExchange()))
                .collect(Collectors.toList());
    }

    /**
     * 融资融券交易汇总
     */
    @GetMapping("margin")
    public List<Margin> getMargin(@RequestParam(value = "trade_date", required = false) Integer trade_date) {
        serviceUtil.initWapRequest("margin");
        if (trade_date != null) {
            serviceUtil.appendField("trade_date", trade_date);
        } else {
            return null;
        }
        WapResponse.WapData data = serviceUtil.getNetworkData(restTemplate);
        return data == null ? null : new ArrayList<>(transformClassUtil.initList(data, Margin.class));
    }

    /**
     * 融资融券交易明细
     */
    @GetMapping("margin_detail")
    public List<MarginDetail> getMarginDetail(@RequestParam(value = "trade_date", required = false) Integer trade_date) {
        serviceUtil.initWapRequest("margin_detail");
        if (trade_date != null) {
            serviceUtil.appendField("trade_date", trade_date);
        } else {
            return null;
        }
        WapResponse.WapData data = serviceUtil.getNetworkData(restTemplate);
        return data == null ? null : new ArrayList<>(transformClassUtil.initList(data, MarginDetail.class));
    }
}
